﻿using Microsoft.AspNetCore.Mvc.Filters;

namespace BasicInformation.ApiWrite.Extensions
{
    public class GlobalException : IExceptionFilter
    {
        private readonly ILogger<GlobalException> logger;

        public GlobalException(ILogger<GlobalException> logger)
        {
            this.logger = logger;
        }

        public void OnException(ExceptionContext context)
        {
            //记录异常信息到日志系统
            logger.LogError("发生全局异常:" +context.Exception.Message);
            //标记异常已处理，防止异常继续向上传播
            context.ExceptionHandled = true;
        }
    }
}
